package top.yehot.cmmu.core.base;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import top.yehot.cmmu.core.enums.JBaseCode;
import top.yehot.cmmu.core.enums.JResCodeEnum;

/**
 * 通用返回信息类
 * 
 * @author lquan
 * @date 2022-9-10 11:37:36
 */
@Getter
@ApiModel("Result对象")
public class JResult<R> {

	@ApiModelProperty("状态码")
	private Integer status;

	@ApiModelProperty("状态码描述")
	private String message;

	@ApiModelProperty("响应信息")
	private R data;

	private JResult(Integer status, String message, R data) {
		this.status = status;
		this.message = message;
		this.data = data;
	}
	
	private static <R> JResult<R> newOf(Integer status, String message, R data) {
		return new JResult<R>(status, message, data);
	}
	
	public static <R> JResult<R> of(Integer status) {
		return JResult.newOf(status, null, null);
	}
	
	public static <R> JResult<R> of(Integer status, String message) {
		return JResult.newOf(status, message, null);
	}
	
	public static <R> JResult<R> of(Integer status, R data) {
		return JResult.newOf(status, null, data);
	}
	
	public static <R> JResult<R> of(Integer status, String message, R data) {
		return JResult.newOf(status, message, data);
	}
	
	public static <R> JResult<R> of(JBaseCode be) {
		return JResult.newOf(be.getStatus(), be.getMessage(), null);
	}
	
	public static <R> JResult<R> of(JBaseCode be, R data) {
		return JResult.newOf(be.getStatus(), be.getMessage(), data);
	}
	
	public static <R> JResult<R> success() {
		return JResult.of(JResCodeEnum.SUCCESS);
	}
	
	public static <R> JResult<R> success(R data) {
		return JResult.of(JResCodeEnum.SUCCESS, data);
	}
	
	public static <R> JResult<R> error() {
		return JResult.of(JResCodeEnum.INNER_ERROR, null);
	}
	
	public static <R> JResult<R> error(R data) {
		return JResult.of(JResCodeEnum.INNER_ERROR, data);
	}

	public static <R> JResult<R> error(String message) {
		return JResult.newOf(JResCodeEnum.INNER_ERROR.getStatus(), message, null);
	}

	public static <R> JResult<R> error(String message, R data) {
		return JResult.newOf(JResCodeEnum.INNER_ERROR.getStatus(), message, data);
	}

}
